package site.marqstree.latte.ec.main.index;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.joanzapata.iconify.widget.IconTextView;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.OnClick;
import site.marqstree.latte.core.delegates.bottom.BottomItemDelegate;
import site.marqstree.latte.core.util.callback.CallbackManager;
import site.marqstree.latte.core.util.callback.CallbackType;
import site.marqstree.latte.core.util.callback.IGlobalCallback;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.ec.R2;
import site.marqstree.latte.ec.main.EcBottomDelegate;
import site.marqstree.latte.ec.main.index.search.SearchDelegate;
import site.marqstree.latte.ec.profile.AppProfile;
import site.marqstree.latte.ui.refresh.RefreshHandler;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.index
 * 文件名: IndexDelegate
 * 创建者: marqstree
 * 创建时间: 2020/1/21 16:23
 * 描述: TODO
 */
public class IndexDelegate extends BottomItemDelegate implements View.OnFocusChangeListener {

    @BindView(R2.id.rv_index)
    RecyclerView mRecyclerView = null;
    @BindView(R2.id.srl_index)
    SwipeRefreshLayout mRefreshLayout = null;
    @BindView(R2.id.tb_index)
    Toolbar mToolbar = null;
    @BindView(R2.id.icon_index_scan)
    IconTextView mIconScan = null;
    @BindView(R2.id.et_search_view)
    EditText mSearchView = null;
    //下拉刷新控件处理器
    private RefreshHandler mRefreshHandler = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_index;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        mSearchView.setOnFocusChangeListener(this);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

        //初始化下拉刷新控件
        initRefreshLayout();

        //初始化RecyclerView
        initRecyclerView();

        //初始化扫描二维码回调接口
        initScanCallback();
    }

    //初始化下拉刷新控件
    private void initRefreshLayout() {
        //设置下拉刷新控件的颜色
        //三种颜色变换
        mRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light
        );
        //设置动画样式下拉的起始点和结束点，scale 是指设置是否需要放大或者缩小动画。
        mRefreshLayout.setProgressViewOffset(true, 120, 300);
    }

    //初始化RecyclerView
    private void initRecyclerView(){
        //定义列表有4列宽
        final GridLayoutManager manager = new GridLayoutManager(getContext(), 4);
        mRecyclerView.setLayoutManager(manager);

        //绑定适配器
        //注意：绑定适配器前，RecyclerView一定要先创建好，否则导致横向布局
        //初始化刷新控件事件处理器
        //将下拉刷新控件绑定到刷新控件事件处理器
        mRefreshHandler = RefreshHandler.create(mRefreshLayout,mRecyclerView,new IndexDataConverter());

        //设置RecyclerView的Item点击事件
        final EcBottomDelegate ecBottomDelegate = getParentDelegate();
        mRefreshHandler.getAdapter().setOnItemClickListener(IndexItemClickListener.create(ecBottomDelegate));

        //获取首页列表数据
        mRefreshHandler.getDataByPage(AppProfile.INDEX_DATA_URL,1);
    }

    @OnClick(R2.id.icon_index_scan)
    void onClickScanQrCode() {
        startScanWithCheck(this.getParentDelegate());
    }

    //初始化扫描二维码回调接口
    private void initScanCallback() {
        CallbackManager.getInstance()
                .addCallback(CallbackType.ON_SCAN, new IGlobalCallback<String>() {
                    @Override
                    public void executeCallback(@Nullable String args) {
                        Toast.makeText(getContext(), "得到的二维码是" + args, Toast.LENGTH_LONG).show();
                    }
                });
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            getParentDelegate().getSupportDelegate().start(new SearchDelegate());
        }
    }
}